<?php

/**
 * This is the model class for table "evaluacion".
 *
 * The followings are the available columns in table 'evaluacion':
 * @property integer $idEvaluacion
 * @property integer $curso_idCurso
 * @property string $fecha
 * @property string $horaInicio
 * @property string $horaFin
 * @property integer $colegios_idColegios
 * @property string $grado
 * @property string $modalidad
 * @property string $nivel
 * @property string $seccion
 * @property string $estado
 * @property string $observacion
 *
 * The followings are the available model relations:
 * @property Curso $cursoIdCurso
 * @property Colegios $colegiosIdColegios
 * @property Usuario[] $usuarios
 * @property Jp[] $jps
 */
class Evaluacion extends CActiveRecord {

    public $estado='Sin Asignar';
    public $horaInicio;
    public $horaFin;


    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'evaluacion';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('curso_idCurso, grado, modalidad, nivel, colegios_idColegios, fecha, horaInicio, horaFin', 'required'),
            array('curso_idCurso, colegios_idColegios', 'numerical', 'integerOnly' => true),
            array('grado, modalidad, nivel, seccion, estado', 'length', 'max' => 50),
            array('observacion', 'length', 'max' => 100),
            array('fecha, horaInicio, horaFin', 'safe'),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('idEvaluacion, curso_idCurso, fecha, horaInicio, horaFin, colegios_idColegios, grado, modalidad, nivel, seccion, estado, observacion', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'curso' => array(self::BELONGS_TO, 'Curso', 'curso_idCurso'),
            'colegios' => array(self::BELONGS_TO, 'Colegios', 'colegios_idColegios'),
            'usuarios' => array(self::MANY_MANY, 'Usuario', 'evaluacion_has_usuario(evaluacion_idEvaluacion, usuario_idUsuario)'),
            'jps' => array(self::MANY_MANY, 'Jp', 'jp_has_evaluacion(evaluacion_idEvaluacion, jp_idJp)'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'idEvaluacion' => 'Id Evaluacion',
            'curso_idCurso' => 'Curso',
            'fecha' => 'Fecha',
            'horaInicio' => 'Hora Inicio',
            'horaFin' => 'Hora Fin',
            'colegios_idColegios' => 'Colegio',
            'grado' => 'Grado',
            'modalidad' => 'Modalidad',
            'nivel' => 'Nivel',
            'seccion' => 'Seccion',
            'estado' => 'Estado',
            'observacion' => 'Observacion',
        );
    }
    
    public function formatea_fecha($fecha){
        if($fecha!=null){
        $nueva_fecha = split('-', $fecha);
        return $nueva_fecha[2].'-'.$nueva_fecha[1].'-'.$nueva_fecha[0];
        }else return null;
    }
    

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search() {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria = new CDbCriteria;
        
        $criteria->compare('idEvaluacion', $this->idEvaluacion);
        //$criteria->compare('curso_idCurso', $this->curso_idCurso);
        $criteria->with =array('curso','colegios');
        $criteria->addSearchCondition('curso.nombreCurso', $this->curso_idCurso);
        $criteria->compare('fecha', $this->fecha, true);
        $criteria->compare('horaInicio', $this->horaInicio, true);
        $criteria->compare('horaFin', $this->horaFin, true);
        //$criteria->compare('colegios_idColegios', $this->colegios_idColegios);
        //$criteria->with =array('colegios');
        $criteria->addSearchCondition('colegios.nombreColegio', $this->colegios_idColegios);
        $criteria->compare('grado', $this->grado, true);
        $criteria->compare('modalidad', $this->modalidad, true);
        $criteria->compare('nivel', $this->nivel, true);
        $criteria->compare('seccion', $this->seccion, true);
        $criteria->compare('estado', $this->estado, true);
        $criteria->compare('observacion', $this->observacion, true);
        $criteria->order='colegios.nombreColegio, fecha, horaInicio';

        return new CActiveDataProvider($this, array(
            'pagination' => array(
             'pageSize' => 25,
        ),
            'criteria' => $criteria,
        ));
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return Evaluacion the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }
    
    

}
